package com.controller.manager;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.service.PjInfoService;

@Controller
@RequestMapping("/pjinfo")
public class PjInfoController {

	@Resource
	private PjInfoService pjInfoService;

	@RequestMapping(value = "/toPjinfoPage")
	public ModelAndView toPjinfoPage(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/pjinfo");
	}

	@RequestMapping(value = "/addPjInfo", method = RequestMethod.POST)
	@ResponseBody
	public String addPjInfo(HttpServletRequest request, @RequestParam Map<String, String> paramsMap,
			@RequestParam(value = "pxtimg", required = false) MultipartFile[] pxtimg,
			@RequestParam(value = "jxtimg", required = false) MultipartFile[] jxtimg) {
		try {
			int result = pjInfoService.addPjInfo(paramsMap, pxtimg, jxtimg);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/getPjInfo", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getPjInfo(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		Map<String, String> pjInfo = pjInfoService.getPjInfoByUuid(paramsMap);
		request.setAttribute("pjInfo", pjInfo);
		return new ModelAndView("/manager/index_two");
	}

	@RequestMapping(value = "/getPjInfoByParam", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getPjInfoByParam(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = pjInfoService.getPjInfoByParam(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/deletePjInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deletePjInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = pjInfoService.deletePjInfo(paramsMap);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/updatePjInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updatePjInfo(HttpServletRequest request, @RequestParam Map<String, String> paramsMap,
			@RequestParam(value = "pxtimg", required = false) MultipartFile[] pxtimg,
			@RequestParam(value = "jxtimg", required = false) MultipartFile[] jxtimg) {
		try {
			int result = pjInfoService.updatePjInfo(paramsMap, pxtimg, jxtimg);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
}
